// pages/login/login.js
// const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

// 默认展示头像 截止到2024年9月上旬
const defaultAvatarUrl = "https://p.ipic.vip/8rt4n5.png"

Page({
  /**
   * 页面的初始数据
   */
  // data: {

  // },
  data: {
    avatarUrl: defaultAvatarUrl,
    screenWidth: 0,
    screenHeight: 0,
    img_id: 1
  },

  // 下头像点击相响应模块
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    // const { avatarUrl } = "/image/5.jpeg"
    this.setData({
      avatarUrl,
    })
  },

  // 画图模块
  onLoad: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        that.setData({
          screenWidth: res.safeArea.width,
          screenHeight: res.safeArea.height,
        });
      },
    })
    const ctx = wx.createCanvasContext("my-canvas", this);
    this.drawImage(ctx);
  },

  // 画图函数，if遍历
  drawImage: function (ctx) {
    const dx = this.data.screenWidth * 0.25;
    const dy = this.data.screenHeight * 0.18;
    // const dw = this.data.screenWidth * 0.5;
    // ctx.drawImage(this.data.avatarUrl,0,0,this.data.avatarUrl.width,this.data.avatarUrl.height,dx,dy,150,150);
    // ctx.scale(2, 2);
    ctx.drawImage(this.data.avatarUrl, 0, 0, 150, 150);
    console.log("头像url")
    console.log(this.data.avatarUrl)
    console.log("头像宽")
    console.log(this.data.avatarUrl.width)
    console.log("高")
    console.log(this.data.avatarUrl.height)
    // ctx.drawImage("/image/5.jpeg", 0, 0, 300, 300, dx, dy, 150, 150);
    if (this.data.img_id == 1) {
      ctx.drawImage("/image/1.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 2) {
      ctx.drawImage("/image/2.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 13) {
      ctx.drawImage("/image/13.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 14) {
      ctx.drawImage("/image/14.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 5) {
      ctx.drawImage("/image/5.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 6) {
      ctx.drawImage("/image/6.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 7) {
      ctx.drawImage("/image/7.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 8) {
      ctx.drawImage("/image/8.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 9) {
      ctx.drawImage("/image/9.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 10) {
      ctx.drawImage("/image/10.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 11) {
      ctx.drawImage("/image/11.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 12) {
      ctx.drawImage("/image/12.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 15) {
      ctx.drawImage("/image/15.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 17) {
      ctx.drawImage("/image/17.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 18) {
      ctx.drawImage("/image/18.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 19) {
      ctx.drawImage("/image/19.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else if (this.data.img_id == 20) {
      ctx.drawImage("/image/20.png", 0, 0, 100, 100, dx, dy, 150, 150);
    } else {
      ctx.drawImage("/image/1.png", 0, 0, 100, 100, dx, dy, 150, 150);
    }

    // ctx.drawImage("/image/1.jpeg", 0, 0, 300, 300, dx, dy, 150, 150);
    console.log(this.data.img_id)
    ctx.draw();
  },

  // 获得权限模块（已弃用）
  getPower: function () {
    let that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              that.saveImage();
            }
          })
        } else {
          wx.openSetting({
            success(res) {
              console.log(res.authSetting)

            }
          })
        }
      }
    })
  },

  // 画图保存模块函数
  saveImage: function () {
    const ctx = wx.createCanvasContext("my-canvas", this);
    this.drawImage(ctx);
    wx.canvasToTempFilePath({
      x: 0,
      y: 365,
      width: 150,
      height: 150,
      destWidth: 900,
      destHeight: 900,
      canvasId: "my-canvas",
      fileType: "png",
      success: function (res) {
        console.log(res, "临时文件");

        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            console.log("succ", res)
          },
          fail(res) {
            console.log("err", res)
          }
        })
      },
      fail: function (res) {
        console.log("err", res);
      },
      complete: function () {

      }
    }, this);

    // 保存提示词
    wx.showToast({
      title: '保存成功',
      icon: 'none',
      duration: 1500,
      success: function () {
        //弹窗后执行，可以省略
        // setTimeout(function () {
        //   wx.reLaunch({
        //     url: '../index/index',
        //   })
        // }, 1500);
      }
    })

  },

  btnTest(e) {
    console.log(e)
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // scroll-view点击响应模块-
  ChangePic(e) {
    this.setData({
      img_id: e.currentTarget.dataset.id
    })
  },

})